package com.wmwj.hxlj.service.impl;

import com.wmwj.hxlj.entity.Article;
import com.wmwj.hxlj.entity.Collection;
import com.wmwj.hxlj.entity.vo.ArticleVo;
import com.wmwj.hxlj.entity.vo.PageResult;
import com.wmwj.hxlj.mapper.ArticleMapper;
import com.wmwj.hxlj.mapper.CollectionFolderMapper;
import com.wmwj.hxlj.mapper.CollectionMapper;
import com.wmwj.hxlj.mapper.UserMapper;
import com.wmwj.hxlj.service.CollectionService;
import com.wmwj.hxlj.utils.BeanCopyUtils;
import com.wmwj.hxlj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionMapper collectionMapper;

    @Autowired
    private CollectionFolderMapper collectionFolderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Result<Integer> add(Collection collection) {
        //查询用户是否存在
        if (userMapper.findById(collection.getUserId()) == null)
            throw new RuntimeException("用户不存在");
        //收藏夹不存在
        if (collectionFolderMapper.findById(collection.getCollectionId()) == null)
            throw new RuntimeException("收藏夹不存在");

        try {
            //通过id查询是否有文章
            Collection co = collectionMapper.findById(collection.getUserId(),collection.getCollectionId()
                    ,collection.getResourceId());
            if(co == null){
                collectionMapper.insert(collection);
                int row =articleMapper.incrementFavorites(collection.getResourceId());
                return Result.success("添加收藏成功",row);
            } else if (co.getStatus() == 0) {
                //存在，取消收藏
                collectionMapper.deleteStatus(collection);
                int row = articleMapper.decrementFavorites(collection.getResourceId());
                return Result.success("取消收藏成功",-row);
            }else{
                //存在，修改收藏状态
                collectionMapper.insertStatus(collection);
                int row = articleMapper.incrementFavorites(collection.getResourceId());
                return Result.success("收藏成功",row);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public Result<PageResult<ArticleVo>> getCollection(Long userId, Long collectionId, Integer page) {
        int pageStart = (page - 1) * 10;
        int pageSize = 10;
        if (userMapper.findById(userId) == null)
            throw new RuntimeException("用户不存在");
        if (collectionFolderMapper.findById(collectionId) == null)
            throw new RuntimeException("收藏夹不存在");
        List<Collection> collections = collectionMapper.findByUserIdAndCollectionId(
                userId,collectionId,pageStart,pageSize);
        if (collections.size() == 0)
//            return Result.success("该收藏夹没有内容");
            throw new RuntimeException("该收藏夹没有内容");

        //将获取到的收藏夹中的resourceid提取出成一个数组
        Long[] ids = collections.stream().map(Collection::getResourceId).toArray(Long[]::new);
        List<Article> articles = articleMapper.findByIds(ids);
        List<ArticleVo> articleVo = BeanCopyUtils.copyBeanList(articles, ArticleVo.class);
        PageResult<ArticleVo> pageResult = new PageResult<>(
                articleVo,collectionMapper.findByUserIdAndCollectionIdCount(userId,collectionId));

        return Result.success(pageResult);
    }
}
